/**
 * Licensed under the Apache License Version 2.0.
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package net.interknit.knitter.cg.template;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import net.interknit.knitter.cg.CGMain;
import net.interknit.knitter.cg.layout.LayoutElement;
import net.interknit.knitter.cg.model.JavaType;
import net.interknit.knitter.cg.view.JavaTypeView;
import net.interknit.knitter.cg.view.JavaTypeViewFactory;
import net.interknit.knitter.cg.view.SeedTypeHolder;

/**
 *
 */
public class CustomTypeDefTemplate extends Template
{
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z");
	public static final String JAVA_CLASS_NAME = "CustomTypeDef";

	@Override
	public String getJavaClassName()
	{
		return JAVA_CLASS_NAME;
	}

	@Override
	public String getJavaPackageName()
	{
		return SeedTypeHolder.getInstance().getSeedTypeView().getJavaPackageName();
	}

	@Override
	public String[] getPlaceholders()
	{
		return new String[] {
				"TIMESTAMP",
				"AUTHOR",
				"GENERATED_CLASS_NAMES_CSV",
		};
	}

	@Override
	public LayoutElement getLayoutElement()
	{
		return null;
	}

	public String TIMESTAMP()
	{
		return sdf.format(new Date());
	}

	public String AUTHOR()
	{
		return CGMain.getInstance().getCGConfig().getAuthor();
	}

	public String GENERATED_CLASS_NAMES_CSV()
	{
		StringBuilder sb = new StringBuilder();

		Map<String, JavaType> defs = CustomTypeDefTemplateHolder.getInstance().getCustomTypeDefs();

		for(String name : CustomTypeDefTemplateHolder.getInstance().getCustomTypeDefNames())
		{
			JavaType def = defs.get(name);

			if(! def.isSeedType())
			{
				JavaTypeView view = JavaTypeViewFactory.create(def);
				String pkgName = view.getJavaPackageName();

				String defName =  (pkgName != null && ! pkgName.isEmpty() ? (pkgName + ".") : "") + view.getJavaClassName();

				sb.append("\t" + "\t" + defName + ".class" + "," + "\n");
			}
		}

		return sb.toString();
	}
}
